package cn.wolfcode.luowowo.website.advice;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ControllerAdvice  controller功能增强标签
 *
 * 在请求进入controller的请求方法之前跟之后，对请求方法进行功能增强
 *
 * 请求方法之前：Date日期格式化
 * 请求方法之后：统一异常处理
 *
 */

@ControllerAdvice
public class CommonExceptionHandler {


    //请求出异常之后，抛出LogicException 统一交给这个方法处理displayExp
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object displayExp(Exception e, HttpServletResponse resp, Model model) throws IOException {
        return new AjaxResult(false, e.getMessage());
    }

    //请求出异常之后，抛出RuntimeException 统一交给这个方法处理runTimeExp
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
       ;
        return new AjaxResult(false, "系统繁忙， 请联系管理员.");
    }
}
